import { Snowflake } from "api/structures/utils/Snowflake";
import { Http, HTTPRequest } from "api/client/http/Http"


export function sendTextMessage(chat_id: string, textMessage: string, callback: (error: Error | null, response: string | null) => void) {

    // 19 numbers
    // 11 75 49 70 89 94 20 28 28 8"
    // 17 10 36 65 61 87 06 91 0
    // 85 27 38 41 51 68 58 83 00

    // 91 83 51 24 94 08 80 82

    // 'content': 'message',
    // 'nonce': str(random.randint(10**18, 10**18 + 2*(10**17))),
    // 'tts': False,

    // {"mobile_network_type":"unknown","content":"1","nonce":"1175497089942028288","tts":false,"flags":0}

    const timespamp = (new Date()).getTime()

    console.log("timestamp " + timespamp)

    const nonce = Snowflake.generate(timespamp)

    console.log("getRandomInt=" + nonce)

    const request: HTTPRequest = {
        method: "POST",
        path: "https://discord.com/api/v9/channels/"+ chat_id +"/messages",
        headers: {
            "Authorization": window.client.token as string,
            // "Referer": "https://discord.com/channels/@me/219007907641950208",
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            mobile_network_type:"unknown",
            content: textMessage,
            nonce: ""+nonce,
            tts:false,
            flags:0
        })
    }

    console.log("request " + JSON.stringify(request))

    Http.request(request, function(err, data){

        if (err) {
            window.client.emit("debug", "Http request " + JSON.stringify(err))
            callback(err, null)
            return;
        }

        window.client.emit("debug", "Http request complete")

        /*
            {
                "id":"1175532651860467734",
                "type":0,
                "content":"Woop",
                "channel_id":"136891423655067648",
                "author":{
                    "id":"118483467766857735",
                    "username":"timosh",
                    "avatar":"643fbd518376a074858bcf41a1c36751",
                    "discriminator":"0",
                    "public_flags":0,
                    "premium_type":0,
                    "flags":0,
                    "banner":null,
                    "accent_color":11382189,
                    "global_name":"Timosh",
                    "avatar_decoration_data":null,
                    "banner_color":"#adadad"
                },
                "attachments":[],
                "embeds":[],
                "mentions":[],
                "mention_roles":[],
                "pinned":false,
                "mention_everyone":false,
                "tts":false,
                "timestamp":"2023-11-18T20:27:04.544000+00:00",
                "edited_timestamp":null,
                "flags":0,
                "components":[],
                "nonce":"1175532651088445400",
                "referenced_message":null
            }
        */

        callback(null, data as string)
    });

}